package com.xigua.principle.inversion;

public class DependencyInversion2 {
    public static void main(String[] args) {
       YeJing yeJing = new YeJing();
//        OpenAndClose openAndClose = new OpenAndClose();
//        openAndClose.open(yeJing);
//        OpenAndClose openAndClose = new OpenAndClose(yeJing);
//        openAndClose.open();
        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.setTv(yeJing);
        openAndClose.open();
    }

}
interface ITV{
    void play();
}
class YeJing implements ITV{

    public void play() {
        System.out.println("打开电视机");
    }
}
//通过接口传递
//interface IOpenAndClose{
//    void open(ITV tv);
//}

//
//class OpenAndClose implements IOpenAndClose{
//    public void open(ITV tv) {
//        tv.play();
//    }
//}

//通过构造方法传递依赖

//interface IOpenAndClose{
//    void open();
//}
//
//class OpenAndClose implements IOpenAndClose{
//    public ITV tv;
//    public OpenAndClose(ITV tv){
//        this.tv = tv;
//    }
//    public void open() {
//        tv.play();
//    }
//}

interface IOpenAndClose{
    void open();
    void setTv(ITV itv);
}

class OpenAndClose implements IOpenAndClose{
    private ITV itv;
    public void open() {
        itv.play();
    }

    public void setTv(ITV itv) {
        this.itv = itv;
    }
}






